package com.zzc.action;

import com.google.gson.JsonObject;
import com.zzc.bean.Dining;
import com.zzc.bean.DiningReceipt;
import com.zzc.bean.Employee;
import com.google.gson.Gson;
import com.zzc.bean.ResultInfo;
import com.zzc.service.*;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class DiningReceiptAction extends BaseAction{

	public DiningReceiptAction() {
		super();
	}
	private DiningReceiptService diningReceiptService;
	private DiningReceipt_sumService diningReceipt_sumService;
	public void setDiningReceipt_sumService(DiningReceipt_sumService diningReceipt_sumService) {
		this.diningReceipt_sumService = diningReceipt_sumService;
	}
	private DiningService diningService;

	public void setDiningService(DiningService diningService) {
		this.diningService = diningService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private EmployeeService employeeService;
	public EmployeeService getEmployeeService() {
		return employeeService;
	}
	public void setDiningReceiptService(DiningReceiptService diningReceiptService) {
		this.diningReceiptService = diningReceiptService;
	}
	public void initData() throws ServletException, IOException {
		diningReceiptService.init();
		getAll();
	}
	private String result_info_add="";
	private ResultInfo resultInfo;
	public void add() throws IOException, ServletException, ParseException {
		resultInfo=new ResultInfo();
		result_info_add=diningReceiptService.add();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
//		HttpServletRequest request = ServletActionContext.getRequest();
//		request.getRequestDispatcher("/view/student/index_employee.jsp").forward(request, getResponse());
		System.out.println("result_info_add:"+result_info_add);
		if (result_info_add.equals("true")){
//            response.sendRedirect(getPath()+"/Action/Action_DiningReceiptAction_get_employee_account");
		}
		else {
			System.out.println("添加数据条件不符合");
//			result_info_add = java.net.URLEncoder.encode(result_info_add,"utf-8");
//            response.sendRedirect(getPath()+"/view/diningReceipt/diningReceipt_add.jsp?result_info_add="+result_info_add);
		}
		resultInfo.setCode(200);
		resultInfo.setData(result_info_add);
		BaseService.printAll_json(resultInfo);
	}
	public void add_autoDining() throws IOException, ServletException, ParseException {
		resultInfo=new ResultInfo();
		Map<String,Object> map=diningReceiptService.add_autoDining();
		result_info_add= (String) map.get("data");
		System.out.println("result_info_add:"+result_info_add);
		if (result_info_add.equals("true")){
			resultInfo.setCode(200);
			int diningReceipt_id= (int) map.get("diningReceipt_id");
			resultInfo.setData(diningReceipt_id);
		}
		else {
			resultInfo.setCode(400);
			resultInfo.setData(result_info_add);
			System.out.println("添加数据条件不符合");
		}
		BaseService.printAll_json(resultInfo);
	}
	public void get_autoDiningLimitedDate() throws IOException, ServletException, ParseException {
		JsonObject jsonObject=diningReceiptService.get_autoDiningLimitedDate();
		BaseService.printAll_json(jsonObject);
	}
	public void delete() throws IOException {
		diningReceiptService.delete();
	}
	public void alter() throws IOException, ServletException {
		diningReceiptService.alter();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.sendRedirect(getPath()+"/Action/Action_DiningReceiptAction_get_employee_account");
	}
	//id废弃
	public void get_employee_id() throws ServletException, IOException {
		List<DiningReceipt> list_diningReceipts = diningReceiptService.get_employee_id();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("list_diningReceipts", list_diningReceipts);
//		BaseService.printAll(list_diningReceipts);
		request.getRequestDispatcher("/view/diningReceipt/diningReceipt_index.jsp").forward(request, getResponse());
	}
	private List filter_allData(HttpServletRequest request,List list_all){
        boolean falg_allData=true;
        if (request.getAttribute("falg_allData")!=null) falg_allData = (boolean) request.getAttribute("falg_allData");
        List list=new ArrayList();
        if (falg_allData){
            return list_all;
        }
        //默认仅显示20条数据,boolean 默认false
        int i=0;
        for (Object object:list_all){
            if (i>9)break;
            list.add(object);
            i++;
        }
        return list;
    }
	public void getAll_list() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
        List list_all = diningReceiptService.getAll();
		BaseService.printAll(filter_allData(request,list_all));
	}
    public void get_list_employee_account() throws ServletException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        Employee employee= (Employee) request.getSession().getAttribute("employee");
        String employee_account=employee.getEmployee_account();

        List list_all = diningReceiptService.get_employee_account(employee_account);
        BaseService.printAll(filter_allData(request,list_all));
    }
	public void getDiningReceipt_sum_list() throws ServletException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        List list_all = diningReceipt_sumService.getDiningReceipt_sum_list();
        BaseService.printAll(filter_allData(request,list_all));
    }
	public void getDiningReceipt_sum_list_nodining_company() throws ServletException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        List list_all = diningReceipt_sumService.getDiningReceipt_sum_list_nodining_company();
        BaseService.printAll(filter_allData(request,list_all));
    }
	public void getDiningReceipt_sum_list_employee_account() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Employee employee= (Employee) request.getSession().getAttribute("employee");
		String employee_account=employee.getEmployee_account();

        List list_all = diningReceipt_sumService.getDiningReceipt_sum_list_employee_account(employee_account);
        BaseService.printAll(filter_allData(request,list_all));
    }
	public void getDiningReceipt_sum_list_employee_account_nodining_company() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Employee employee= (Employee) request.getSession().getAttribute("employee");
		String employee_account=employee.getEmployee_account();

        List list_all = diningReceipt_sumService.getDiningReceipt_sum_list_employee_account_nodining_company(employee_account);
        BaseService.printAll(filter_allData(request,list_all));
    }
	public void getAll() throws ServletException, IOException {
		List list_diningReceipts = diningReceiptService.getAll();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("list_diningReceipts", list_diningReceipts);
//		BaseService.printAll(list_diningReceipts);
		request.getRequestDispatcher("/view/diningReceipt/diningReceipt_index.jsp").forward(request, getResponse());
	}
    public void get_employee_account() throws ServletException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        Employee employee= (Employee) request.getSession().getAttribute("employee");
        String employee_account=employee.getEmployee_account();
        List<?> list_diningReceipts = diningReceiptService.get_employee_account(employee_account);
        request.setAttribute("list_diningReceipts", list_diningReceipts);
//		BaseService.printAll(list_diningReceipts);
        request.getRequestDispatcher("/view/diningReceipt/diningReceipt_index.jsp").forward(request, getResponse());
    }
	public void alter_get_id() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		DiningReceipt diningReceipt_alter=diningReceiptService.alter_get_id();
		request.setAttribute("diningReceipt_alter",diningReceipt_alter);
		request.getRequestDispatcher("/view/diningReceipt/diningReceipt_alter.jsp").forward(request, getResponse());
	}
//	public void studentSC() throws ServletException, IOException {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		List<Dining> list_courses_xuanxiu1= diningService.getCourses_xuanxiu1();
//		List<Dining> list_courses_xuanxiu2= diningService.getCourses_xuanxiu2();
//		request.setAttribute("list_courses_xuanxiu1",list_courses_xuanxiu1);
//		request.setAttribute("list_courses_xuanxiu2",list_courses_xuanxiu2);
//		request.getRequestDispatcher("/view/student/diningReceipt_lists.jsp").forward(request, getResponse());
//	}
	public void getSCs_courseName() throws IOException, ServletException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String coutse_json= (String) session.getAttribute("coutse_json");
		Dining dining =new Gson().fromJson(coutse_json, Dining.class);
//		List<DiningReceipt> list_diningReceipts = diningReceiptService.getSCs_courseName(dining.getName());
//		request.setAttribute("list_diningReceipts", list_diningReceipts);
//		request.getRequestDispatcher("/view/teacher/teacherCS.jsp").forward(request, getResponse());
//		return "getSCs";
	}
}
